package m.jclecai.com.activity.follow;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.fragment.follow.CustomOrderFragment;
import m.jclecai.com.fragment.refreshableList.MineConcernListFragment;
import m.jclecai.com.fragment.refreshableList.MineFollowListFragment;
import m.jclecai.com.fragment.refreshableList.MyPlanListFragment;
import m.jclecai.com.listener.Callback;

/**
 * 这里是我的关注的页面的逻辑;
 */
public class MineConcernActivity extends BaseActivity {

    @BindView(R.id.tl_tablayout)
    TabLayout mTlTablayout;

    @BindView(R.id.vp_view_pager)
    ViewPager mVpViewPager;

    private CharSequence[] mStrings = {"我的关注", "定制跟单", "我的跟单","我的方案"};
    private ArrayList<BaseFragment> mConcernsFragments;
    private MyPagerAdapter mMyPagerAdapter;


    @Override
    protected int getContentView() {
        return R.layout.activity_mine_concern;
    }

    @Override
    protected void initView() {
        super.initView();

        setTitle("我的战绩");
        leftOptionText("");
    }

    @Override
    protected void initData() {
        super.initData();

        checkLogin(new Callback(){

            @Override
            public void callbackSuccess() {
                super.callbackSuccess();


                initFragments();

                mMyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
                mVpViewPager.setAdapter(mMyPagerAdapter);
                mVpViewPager.setOffscreenPageLimit(2);

                mTlTablayout.setupWithViewPager(mVpViewPager);

            }

            @Override
            public void callbackField() {
                super.callbackField();
                finish();
            }
        });
    }

    /**
     * 初始化所有的fragments
     */
    private void initFragments() {
        mConcernsFragments=new ArrayList<>();
        mConcernsFragments.add(new MineConcernListFragment());
        mConcernsFragments.add(new CustomOrderFragment());
        mConcernsFragments.add(new MineFollowListFragment());
        mConcernsFragments.add(new MyPlanListFragment());
    }



    @Override
    protected void initListener() {
        super.initListener();
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return mStrings[position];
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mConcernsFragments.get(position);
        }
        @Override
        public int getCount() {
            return mStrings.length;
        }
    }
}
